package com.jiantong.web.open.app.work;

import com.google.common.collect.Maps;
import com.jiantong.common.util.UserUtil;
import com.jiantong.user.ext.LoginUser;
import com.jiantong.web.core.enums.ExceptionCode;
import com.jiantong.work.model.MarkWearManagement;
import com.jiantong.work.service.MarkWearManagementReadService;
import com.jiantong.work.service.MarkWearManagementWriteService;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPClientException;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;
import java.util.Map;

/**
 * Created by mz03 on 2017/7/20.
 */
@OpenBean
@Slf4j
public class MarkWearManagementApis {
    @RpcConsumer
    private MarkWearManagementReadService markWearManagementReadService;

    @RpcConsumer
    private MarkWearManagementWriteService markWearManagementWriteService;


    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @OpenMethod(
            key = "find.mark.wear.management.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public MarkWearManagement findById(Long id){

        if(id == null){
            log.error("id should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "id should be not null");
        }

        Response<MarkWearManagement> response = markWearManagementReadService.findById(id);

        if(!response.isSuccess()){
            log.error("failed to find mark wear management by id = {}",id);
            throw new OPServerException(ExceptionCode.DATA_NOT_FOUND.value(),
                    "failed to find mark wear management mark");
        }
        return response.getResult();
    }

    /**
     *  创建
     * @param markWearManagement
     * @return
     */
    @OpenMethod(
            key = "create.mark.wear.management.mark",
            paramNames = {"markWearManagement"},
            httpMethods = RequestMethod.POST
    )
    public Boolean create(MarkWearManagement markWearManagement){
        if(markWearManagement == null){
            log.error("this object should be not null");
            throw new OPClientException(ExceptionCode.OBJECT_NOT_EXIST.value(),
                    "this object should be not null");
        }
        Response<Boolean> response = markWearManagementWriteService.create(markWearManagement);
        if(!response.isSuccess()){
            log.error("create animal mark");
            throw new OPServerException(ExceptionCode.DATA_WRITE_FAIL.value(),"create animal mark");
        }
        return response.getResult();
    }

    /**
     * 更新
     * @param markWearManagement
     * @return
     */
    @OpenMethod(
            key = "update.mark.wear.management",
            paramNames = {"markWearManagement"},
            httpMethods = RequestMethod.PUT
    )
    public Boolean updateAnimalMark(MarkWearManagement markWearManagement){
        if(markWearManagement.getId() == null){
            log.error("id should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "id should be not null");
        }
        Response<Boolean> response = markWearManagementWriteService.update(markWearManagement);
        if(!response.isSuccess()){
            log.error("failed to update animal mark");
            throw new OPServerException(ExceptionCode.DATA_UPDATE_FAIL.value(),
                    "failed to update animal mark");
        }
        return response.getResult();
    }

    /**
     * 根据时间和批号分组,分页查看当前登录人管辖养殖场
     * @param pageNo
     * @param pageSize
     * @return
     */
    @OpenMethod(
            key = "paging.mark.wear.management.by.user.login",
            paramNames = {"pageNo","pageSize","breedFactoryName"},
            httpMethods = RequestMethod.GET
    )
    public Paging<MarkWearManagement> pagingMarkWearManagement(Integer pageNo , Integer pageSize,String breedFactoryName){
        LoginUser loginUser = UserUtil.getCurrentUser();
        List<Long> breedFactoryIds = loginUser.getBreedFactoryIds();
        if (breedFactoryIds == null){
            throw new OPServerException(ExceptionCode.PARAMETER_NOT_EXIST.value(),"login user no farm management");
        }
        Map<String , Object> map = Maps.newHashMap();
        map.put("breedFactoryIds",breedFactoryIds);
        if (breedFactoryName != null && !breedFactoryName.equals("")){
            map.put("breedFactoryName",breedFactoryName);
        }
        Response<Paging<MarkWearManagement>> response = markWearManagementReadService.pagingMarkWearManagement(pageNo,pageSize,map);
        if(!response.isSuccess()){
            log.error("failed to find mark wear management");
            throw new OPServerException(ExceptionCode.OBJECT_NOT_EXIST.value(),
                    "failed to find mark wear management");
        }
        return response.getResult();

    }

}
